const fs = require('fs')

var readFile = fileName => {
  return new Promise(function (resolve, reject) {
    fs.readFile(fileName, (err, data) => {
      if (err) reject(err)
      console.log(data.toString())
      resolve(data)
    })
  })
}

var syncAA = async () => {
  var r = await readFile('./package.json')
  var a = await readFile('./db.json')
  return a
}

var rst = syncAA()
rst
  .then(r => {
    console.log('r==', r.toString())
  })
  .catch(err => {
    console.log('err=', err)
  })


// ********************************************


function timeout (ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

async function asyncPrint (value, ms) {
  await timeout(ms).catch(err => {
    console.log(err)
  });
  console.log(value)
}

asyncPrint('hello world', 2000);


// ********************************************

var gen = function* () {
  var f1 = yield readFile('./db.json')
  var f2 = yield readFile('./package.json')
  console.log(f1);
  console.log(f2);
}
var g = gen()
console.log(g.next())
console.log(g.next())